import baseApi from '../utils/base.api.ts'
import { saveRepository } from '../../api/interface/demo.ts'

/**
 * It is recommended that the classes that process data on the back end inherit the abstract class baseApi.
 * If you want the specified data to trigger clear when the page is destroyed,
 * please call the init function for initialization after importing.
 * such as:
 * import { publicStore } from '@/service'
 * publicStore.init()
 */
class PublicStore extends baseApi {
  repositoryInfo = new Map() // 资源库的信息
  repository = new Map() // 资源库缩略图
  friendsFolder = []
  total = null // 资源库总数

  // 新增素材库
  async saveRepository(data: any) {
    try {
      let cacheData: any = {
        folderName: data.folderName,
        folderType: data.folderType,
        parentId: data.parentId,
        folderDescribe: data.describe,
      }
      let result = await saveRepository(cacheData)
      cacheData['id'] = result.data
      this.repository.set(result.data, [])
      this.repositoryInfo.set(result.data, {
        folderDescribe: cacheData.folderDescribe,
        id: result.data,
        folderName: cacheData.folderName,
      })
      return Promise.resolve(cacheData)
    } catch (err) {
      return Promise.reject(err)
    }
  }

  override clear() {
    this.friendsFolder.length = 0
  }
}

export const publicStore = new PublicStore()
